package com.dianning.service.imp;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

import org.apache.commons.logging.*;

import com.dianning.bean.AdSpace;
import com.dianning.bean.Money;
import com.dianning.bean.MoneyLog;
import com.dianning.bean.Visitor;
import com.dianning.dao.AdAdsDAO;
import com.dianning.dao.AdSpaceDAO;
import com.dianning.dao.MoneyDAO;
import com.dianning.dao.MoneyLogDAO;
import com.dianning.dao.VisitorDAO;
import com.dianning.exception.DianningException;
import com.dianning.service.AdService;
import com.dianning.service.MoneyService;
import com.dianning.service.VisitorService;
import com.dianning.service.web.PageList;
import com.dianning.service.web.Pages;

public class MoneyServiceImp implements MoneyService {

	private static final Log logger = LogFactory.getLog(MoneyServiceImp.class);
	
	private MoneyDAO moneyDAO;
	private MoneyLogDAO moneyLogDAO;

	public MoneyDAO getMoneyDAO() {
		return moneyDAO;
	}

	public void setMoneyDAO(MoneyDAO moneyDAO) {
		this.moneyDAO = moneyDAO;
	}

	public MoneyLogDAO getMoneyLogDAO() {
		return moneyLogDAO;
	}

	public void setMoneyLogDAO(MoneyLogDAO moneyLogDAO) {
		this.moneyLogDAO = moneyLogDAO;
	}

	/**
	 * 根据主键ID获取Money对象
	 * 
	 * @param uid
	 * 			主键ID
	 * @return Money
	 * 			Money对象
	 */
	public Money findMoneyByUid(long uid){
		return this.getMoneyDAO().findMoneyByUid(uid);
	}
	
	/**
	 * 更新Money对象
	 * @param money
	 * @param moneyLog
	 * @return Money
	 */
	public Money updateMoney(Money money,MoneyLog moneyLog){
		money = this.getMoneyDAO().updateMoney(money);
		if(moneyLog != null){
			this.getMoneyLogDAO().saveMoneyLog(moneyLog);
		}
		return money;
	}
	
	/**
	 * 获取MoneyLog对象列表
	 * @param uid
	 * @param startTime
	 * @param endTime
	 * @param logtype
	 * @param orderby
	 * @param ascOrDesc
	 * @param pages
	 * @return PageList
	 */
	public PageList findMoneyLogs(long uid,Date startTime,Date endTime,String logtype,String orderby, int ascOrDesc,Pages pages){
		PageList pl = new PageList();
        if (pages.getTotalNum() == -1) {
            pages.setTotalNum(this.getMoneyLogDAO().getMoneyLogNum(uid, startTime, endTime, logtype));
        }
        pages.executeCount();
        if(pages.getTotalNum() == 0){
        	pl.setObjectList(null);
        	pl.setPages(pages);
        	return pl;
        }
		List l = this.getMoneyLogDAO().findMoneyLogs(uid, startTime, endTime, logtype, orderby, ascOrDesc,pages.getSpage(), pages.getPerPageNum());
		pl.setObjectList(l);
	    pl.setPages(pages);
	    return pl;
	}
}
